package com.pad.pcbuilder;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import com.pad.pcbuilder.FirstActivity.MyAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.view.View;
import android.widget.ImageButton;

public class ConfgActivity extends Activity {
	
	private static ArrayAdapter<String> adapter;
	private String[] prueba;
	private String read, write;
	private Spinner s;
	public static Activity vista=null;
	private TextView textViewToChange, textViewToChange0, textViewToChange2, textViewToChange3, textViewToChange4, textViewToChange5, textViewToChange6, textViewToChange7, textViewToChange8, textViewToChange9, textViewToChange10, textViewToChange11;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		vista = this;
		setContentView(R.layout.activity_confg);
		
		textViewToChange0 = (TextView) findViewById(R.id.textView10c);
		textViewToChange0.setText(R.string.textView10c);
		
		s = (Spinner) findViewById(R.id.spinner1);
		s.setOnItemSelectedListener(new OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
				String[] inter = prueba[position].split(";");
				
				String socket = inter[1];
				String  cpu = inter[2];
				
				String placa = inter[3];
				
				String  ram = inter[4];
				
				String  disco = inter[5];

				String  gpu = inter[6];
				
				String  fuente = inter[7];
				
				String  disip = inter[8];
				
				String  lec = inter[9];
				
				String  caj = inter[10];
				
				String precio = inter[11];
				
				String mystring = getResources().getString(R.string.TextViewsocket);	
				
				String mystring2 = getResources().getString(R.string.TextViewcpu);	
				
				String mystring3 = getResources().getString(R.string.TextViewplaca);
				
				String mystring4 = getResources().getString(R.string.TextViewram);	
				
				String mystring5 = getResources().getString(R.string.TextViewdisco);
				
				String mystring6 = getResources().getString(R.string.TextViewgpu);	
				
				String mystring7 = getResources().getString(R.string.TextViewfuente);
				
				String mystring8 = getResources().getString(R.string.TextViewdis);	
				
				String mystring9 = getResources().getString(R.string.TextViewlector);	
				
				String mystring10 = getResources().getString(R.string.TextViewcaja);	
				
				textViewToChange = (TextView) findViewById(R.id.textView11);
				textViewToChange.setText(mystring+": "+ socket);
				
				textViewToChange2 = (TextView) findViewById(R.id.texto);
				textViewToChange2.setText(mystring2 +": "+ cpu);
				
				textViewToChange3 = (TextView) findViewById(R.id.textView3);
				textViewToChange3.setText(mystring3+": "+ placa);

				textViewToChange4 = (TextView) findViewById(R.id.TextView01);
				textViewToChange4.setText(mystring4+": "+ ram);
				
				textViewToChange5 = (TextView) findViewById(R.id.textView2);
				textViewToChange5.setText(mystring5+": "+ disco);
				
				textViewToChange6 = (TextView) findViewById(R.id.textView4);
				textViewToChange6.setText(mystring6+": "+ gpu);
				
				textViewToChange7 = (TextView) findViewById(R.id.textView5);
				textViewToChange7.setText(mystring7+": "+ fuente);
				
				textViewToChange8 = (TextView) findViewById(R.id.textView6);
				textViewToChange8.setText(mystring8+": "+ disip);

				textViewToChange9 = (TextView) findViewById(R.id.textView7);
				textViewToChange9.setText(mystring9+": "+ lec);
				
				textViewToChange10 = (TextView) findViewById(R.id.textView8);
				textViewToChange10.setText(mystring10+": "+ caj);
				
				textViewToChange11 = (TextView) findViewById(R.id.textView10);
				textViewToChange11.setText(precio);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}             
		});
		readData();
		if (!read.equalsIgnoreCase("")){
			construir();
		}else{
			vacio();
		}
		
		final ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
		button2.setOnClickListener(new View.OnClickListener() {
	           public void onClick(View v) {
	        	borrar();
	           }
	       });
		
		final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
	       	button.setOnClickListener(new View.OnClickListener() {
	           	public void onClick(View v) {
	           		finish();
	           	}
	       	});
	     
           

	}
	
	private void construir(){
		ArrayList<String> nombres = new ArrayList<String>();
		this.prueba = read.split("\\*");
		String[] inter = this.prueba[0].split(";");
		String[] inter2;
		for (int i = 0; i < this.prueba.length; i++){
			inter2 = this.prueba[i].split(";");
			String nombre = inter2[0];
			nombres.add(nombre);
		}
		s = (Spinner) findViewById(R.id.spinner1);
		adapter = new ArrayAdapter<String>(ConfgActivity.this, android.R.layout.simple_spinner_item, nombres);
		s.setAdapter(adapter);
		
		String socket = inter[1];
		
		String  cpu = inter[2];
		
		String placa = inter[3];
		
		String  ram = inter[4];
		
		String  disco = inter[5];

		String  gpu = inter[6];
		
		String  fuente = inter[7];
		
		String  disip = inter[8];
		
		String  lec = inter[9];
		
		String  caj = inter[10];
		
		String precio = inter[11];
		
		textViewToChange = (TextView) findViewById(R.id.textView11);
		textViewToChange.setText(socket);
		
		textViewToChange2 = (TextView) findViewById(R.id.texto);
		textViewToChange2.setText(cpu);
		
		textViewToChange3 = (TextView) findViewById(R.id.textView3);
		textViewToChange3.setText(placa);

		textViewToChange4 = (TextView) findViewById(R.id.TextView01);
		textViewToChange4.setText(ram);
		
		textViewToChange5 = (TextView) findViewById(R.id.textView2);
		textViewToChange5.setText(disco);
		
		textViewToChange6 = (TextView) findViewById(R.id.textView4);
		textViewToChange6.setText(gpu);
		
		textViewToChange7 = (TextView) findViewById(R.id.textView5);
		textViewToChange7.setText(fuente);
		
		textViewToChange8 = (TextView) findViewById(R.id.textView6);
		textViewToChange8.setText(disip);

		textViewToChange9 = (TextView) findViewById(R.id.textView7);
		textViewToChange9.setText(lec);
		
		textViewToChange10 = (TextView) findViewById(R.id.textView8);
		textViewToChange10.setText(caj);
		
		textViewToChange11 = (TextView) findViewById(R.id.textView10);
		textViewToChange11.setText(precio);
		
	}
	
	private void readData(){
		BufferedReader buff = null;
		String result = "";
		try {
			FileInputStream inputStream = openFileInput("config.txt");
				buff = new BufferedReader(new InputStreamReader(inputStream));
				String line;
				while ((line = buff.readLine()) != null) {
					result = result + line;
				}
				read = result;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				buff.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private void vacio(){
				
		ArrayList<String> nombres = new ArrayList<String>();
		s = (Spinner) findViewById(R.id.spinner1);
		adapter = new ArrayAdapter<String>(ConfgActivity.this, android.R.layout.simple_spinner_item, nombres);
		s.setAdapter(adapter);
		
		textViewToChange = (TextView) findViewById(R.id.textView11);
		textViewToChange.setText("");
		
		textViewToChange2 = (TextView) findViewById(R.id.texto);
		textViewToChange2.setText("");
		
		textViewToChange3 = (TextView) findViewById(R.id.textView3);
		textViewToChange3.setText("");

		textViewToChange4 = (TextView) findViewById(R.id.TextView01);
		textViewToChange4.setText("");
		
		textViewToChange5 = (TextView) findViewById(R.id.textView2);
		textViewToChange5.setText("");
		
		textViewToChange6 = (TextView) findViewById(R.id.textView4);
		textViewToChange6.setText("");
		
		textViewToChange7 = (TextView) findViewById(R.id.textView5);
		textViewToChange7.setText("");
		
		textViewToChange8 = (TextView) findViewById(R.id.textView6);
		textViewToChange8.setText("");

		textViewToChange9 = (TextView) findViewById(R.id.textView7);
		textViewToChange9.setText("");
		
		textViewToChange10 = (TextView) findViewById(R.id.textView8);
		textViewToChange10.setText("");
		
		textViewToChange11 = (TextView) findViewById(R.id.textView10);
		textViewToChange11.setText("");
		
	}
	
	public void borrar(){
		int borrar = s.getSelectedItemPosition();
		write = "";
		if (!read.equalsIgnoreCase("")){
			for (int i = 0; i < prueba.length; i++){
				if (i != borrar){
					write = write + prueba[i].toString() + "*";
				}
			}
			writeData(write);
			read = write;
			if (!read.equalsIgnoreCase("")){
				construir();
			}else{
				vacio();
			}
		}
	}
	
	private void writeData(String data){
		BufferedWriter buff = null;
		try {
			FileOutputStream outputStream = openFileOutput("config.txt", Context.MODE_PRIVATE);
			buff = new BufferedWriter(new OutputStreamWriter(outputStream));
			buff.write(data,0,data.length());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				buff.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
